/*
* Copyright (C) 2013 Nivis LLC.
* Email:   opensource@nivis.com
* Website: http://www.nivis.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
* 
* Redistribution and use in source and binary forms must retain this
* copyright notice.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/*
 * Manager.h
 *
 *  Created on: Dec 15, 2008
 *      Author: Radu Pop
 */

#ifndef MANAGER_H_
#define MANAGER_H_

#include <nlib/log.h>
#include <WHartStack/WHartStack.h>

#include <Common/NEAddress.h>
#include <Model/Capabilities.h>
#include <Model/NetworkEngine.h>
#include "../../NMSettingsLogic.h"
#include "../../Security/SecurityManager.h"
#include "ManagerStack.h"
#include "operations/WHOperationQueue.h"
#include "operations/WHEngineOperationsVisitor.h"

#include "CommandsProcessor.h"
#include "DevicesManager.h"
#include "PeriodicTaskManager.h"
#include "NodeVisibleVerifier.h"

#include "alarms/AlarmDispatcher.h"
#include "reports/ReportDispatcher.h"

#include "WritePIDFile.h"

using namespace hart7::nmanager::operations;

namespace hart7 {

namespace nmanager {

/**
 * Manager aggregator class. Creates stack, engine, device manager, etc.
 */
class Manager
{
        LOG_DEF("h7.n.Manager")
        ;

    public:

        Manager(NetworkEngine& networkEngine, hart7::security::SecurityManager& securityManager,
                hart7::util::NMSettingsLogic& settings);

        virtual ~Manager()
        {
        }

        /**
         * Starts main loop.
         */
        void Start();

        /**
         * Stops main loop.
         */
        void Stop();

        /**
         * Handle HUP signal to reload configuration. Called after the config files have been re-read.
         */
        void HandleHUP();

        /**
         * Called from MainApp when a SIGHUP is received. Marks for reload.
         */
        void ManagerMarkForReloadingConfigFilesHandler();

        /**
         * Reload config loopback. Used to notify MainApp to actually re-read the configs.
         */
        void ManagerReloadConfigFilesHandler();

        boost::function0<void> mainAppReloadConfigFilesHandler;

    private:

        Address64 whManagerAddress64;

        Address64 whGatewayAddress64;

        /**
         * The reference to the NetworkEngine.
         */
        NetworkEngine& networkEngine;

        /**
         * Security Manager
         */
        hart7::security::SecurityManager& securityManager;

        /**
         *
         */
        hart7::util::NMSettingsLogic& settings;

        /**
         * Reference to the stack.
         */
        ManagerStack stack;

        AlarmDispatcher alarmDispatcher;

        ReportDispatcher reportDispatcher;

        /**
         *
         */
        CommonData commonData;

        /**
         * The list with all the operations  generated by NetworkEngine.
         */
        hart7::nmanager::operations::WHOperationQueue operationQueue;

        CommandsProcessor commandsProcessor;

        DevicesManager devicesManager;

        PeriodicTaskManager periodicTask;

        NodeVisibleVerifier nodeVisibleVerifier;

        WritePIDFile writePidFile;
};

} // namespace nmanager
} // namespace hart7


#endif /* MANAGER_H_ */
